package com.example.demo.service.impl;

import com.example.demo.entity.MonitorcodeInfo;
import com.example.demo.entity.MonitorobjInfo;
import com.example.demo.service.TestAccessService;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetAddress;

/**
 * 验证IP是否可以联通的实现类
 * Created by tanjun on 2021-12-30.
 */
@Component
public class TestIPAccessService extends BaseAccessService implements TestAccessService {

    public static void main(String[] args) {
        TestIPAccessService testservice=new TestIPAccessService();
        testservice.testAccess("");
    }

    public void testAccess(String monitorObjId){
        //从数据库获取IP信息
        MonitorobjInfo monitorobjInfo=monitorobjInfoMapper.selectByPrimaryKey(monitorObjId);
        if(monitorobjInfo.getMonitorobjCodeId()==null){
            return;
        }
        MonitorcodeInfo monitorcodeInfo=monitorcodeInfoMapper.selectByPrimaryKey(monitorobjInfo.getMonitorobjCodeId());
        try {
            InetAddress address = InetAddress.getByName("web.mit.edu");
            if(address.isReachable(4000)){
                accessTestResultHandler.handlerAccessTestSuccess(monitorObjId,monitorcodeInfo.getMonitorcodeId());
            }else{
                accessTestResultHandler.handlerAccessTestfailure(monitorObjId,monitorcodeInfo.getMonitorcodeId());
            }
        } catch (IOException e) {
            e.printStackTrace();
            accessTestResultHandler.handlerAccessTestfailure(monitorObjId,monitorcodeInfo.getMonitorcodeId());
        }

    }
}
